﻿
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System;

namespace FrameWorkSong
{
    public class UIManager
    {
        private  UIManager()
        {
            ParseUIPanelTypeJson();
        }
        private static Transform canvasTransform;
        public  static Transform CanvasTransform
        {
            get
            {
                if (canvasTransform == null)
                {
                        canvasTransform = GameObject.Find("Canvas").transform;
                }
                return canvasTransform;
            }
        }


        private static Dictionary<UIPanelType, string> panelPathDict;//存储面板路径
        private static Dictionary<UIPanelType, BasePanel> panelDict;
        private static Stack<BasePanel> panelStack;//面板栈，管理显示的面板

        /// <summary>
        /// 面板入栈
        /// </summary>
        /// <param name="uIPanelType"></param>
        public static void PushPanel(UIPanelType uIPanelType)
        {

            if (panelStack == null)
            {
                panelStack = new Stack<BasePanel>();
            }
            //栈顶页面暂停
            if (panelStack.Count > 0)
            {
                BasePanel topPanel = panelStack.Peek();
                topPanel.OnPause();
            }

            BasePanel basePanel = GetPanel(uIPanelType);
            basePanel.OnEnter();
            panelStack.Push(basePanel);
               
        }
        /// <summary>
        /// 面板出栈
        /// </summary>
        public static void PopPanel()
        {
            if (panelStack == null)
            {
                panelStack = new Stack<BasePanel>();
            }
            //如果栈内有面板
            if (panelStack.Count <= 0) return;
            //移除该栈
            BasePanel topPanel = panelStack.Pop();
            topPanel.OnExit();
            // 如果栈内还有面板
            if (panelStack.Count <= 0) return;
            //激活该栈
            BasePanel topPanel2 = panelStack.Peek();
            topPanel2.OnResume();
        }
        /// <summary>
        /// 得到面板
        /// </summary>
        /// <param name="uIPanelType"></param>
        /// <returns></returns>
        private static BasePanel GetPanel(UIPanelType uIPanelType)
        {
            if (panelDict == null)
            {
                panelDict = new Dictionary<UIPanelType, BasePanel>();
            }
            BasePanel basePanel = panelDict.TryGat(uIPanelType);

            if (basePanel == null)
            {
                if (panelPathDict == null)
                {
                    ParseUIPanelTypeJson();
                }
                string path = panelPathDict.TryGat(uIPanelType);
                GameObject instPanel = GameObject.Instantiate(Resources.Load(path)) as GameObject;
                instPanel.transform.SetParent(CanvasTransform, false);
                panelDict.Add(uIPanelType, instPanel.GetComponent<BasePanel>());
                basePanel = panelDict.TryGat(uIPanelType);
            }
            return basePanel;

        }
        /// <summary>
        /// 解析UI面板信息
        /// </summary>
        private static void ParseUIPanelTypeJson()
        {
            if (panelPathDict == null)
            {
                panelPathDict = new Dictionary<UIPanelType, string>();
            }
            TextAsset jsonText = Resources.Load<TextAsset>("Config/UIPanelTypeJson");
            JsonData jsonData = JsonMapper.ToObject(jsonText.text);
            int length = jsonData.Count;
            for (int i = 0; i < length; i++)
            {
                UIPanelInfo uIPanel = new UIPanelInfo();
                uIPanel.PanelTypeString = jsonData[i]["panelType"].ToString();
                uIPanel.Path = jsonData[i]["path"].ToString();
                panelPathDict.Add(uIPanel.panelType, uIPanel.Path);
            }
        }

    }

    public class BasePanel : MonoBehaviourSimplify
    {
       protected  CanvasGroup canvasGroup;
        void Start()
        {
            if (canvasGroup == null)
            {
                gameObject.AddComponent<CanvasGroup>();
                canvasGroup = GetComponent<CanvasGroup>();
            }
        }
        /// <summary>
        /// 进入
        /// </summary>
        public virtual void OnEnter()
        {
            if (canvasGroup == null)
            {
                gameObject.AddComponent<CanvasGroup>();
                canvasGroup = GetComponent<CanvasGroup>();
            }
        }
        /// <summary>
        /// 暂停
        /// </summary>
        public virtual void OnPause(){}
        /// <summary>
        /// 继续
        /// </summary>
        public virtual void OnResume(){}
        /// <summary>
        /// 退出
        /// </summary>
        public virtual void OnExit(){}
        public override void OnBeforeDestroy(){}

        /// <summary>
        /// UI面板名称转枚举
        /// </summary>
        /// <param name="PanelName"></param>
        /// <returns></returns>
        protected UIPanelType GetPanelType(string PanelName)
        {
            UIPanelType uIPanelType = (UIPanelType)Enum.Parse(typeof(UIPanelType), PanelName);
            return uIPanelType;
        }
    }

    [Serializable]
    public class UIPanelInfo
    {
        // [NonSerialized]
        public UIPanelType panelType;
        private string path;
        public string PanelTypeString
        {
            get
            {
                return panelType.ToString();
            }
            set
            {
                panelType = (UIPanelType)Enum.Parse(typeof(UIPanelType), value);
            }
        }
        public string Path
        {
            get
            {
                return path;
            }

            set
            {
                path = value;
            }
        }
    }
}
